/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.UserBeanLocal;
import entities.User;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.primefaces.event.RowEditEvent;
import web.models.UserModel;

/**
 *
 * @author Jesus
 */
@ManagedBean
public class AdmUsersBean {

    @EJB
    private UserBeanLocal userBean;
    private UserModel users;
    private String adminName;

    public AdmUsersBean() {
    }

    @PostConstruct
    public void init() {
        users = new UserModel(userBean.findAll());
    }

    public UserModel getUsers() {
        return users;
    }

    public void setUsers(UserModel users) {
        this.users = users;
    }

    public SelectItem[] getAdmins() {
        return new SelectItem[]{
            new SelectItem(1),
            new SelectItem(0)};
    }

    public void quitAdmin(int rowVar) {
        System.out.println("ROW:" + rowVar);
        User user = ((List<User>) users.getWrappedData()).get(rowVar);
        user.setIsadmin(Short.valueOf("0"));
        user = userBean.update(user);
        if (user != null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "El usuario " + user.getEmail() + " ha sido actualizado", ""));
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ha ocurrido un error al actualizar el usuario.", ""));
        }
    }

    public void makeAdmin(int rowVar) {
        System.out.println("ROW:" + rowVar);
        User user = ((List<User>) users.getWrappedData()).get(rowVar);
        user.setIsadmin(Short.valueOf("1"));
        user = userBean.update(user);
        if (user != null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "El usuario " + user.getEmail() + " ha sido actualizado", ""));
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ha ocurrido un error al actualizar el usuario.", ""));
        }
    }
}
